package com.autonavi.yunda.yunji.core.service.thirdpartyservice.vo;

import com.autonavi.yunda.yunji.core.model.dto.PartUnitDTO;
import com.autonavi.yunda.yunji.core.model.vo.ComponentData;
import com.autonavi.yunda.yunji.core.vo.response.FalconOptionsVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.util.List;
import java.util.stream.Collectors;

import static com.autonavi.yunda.yunji.core.model.constants.PartUnitConstants.FALCON_SOURCE_TYPE;
import static com.autonavi.yunda.yunji.core.model.constants.PartUnitConstants.SOURCE_PREFIX;

/**
 * Description:api部件字段
 *
 * @author cizhong.wcs
 * @date 2023/1/5 20:44
 */
@EqualsAndHashCode(callSuper = true)
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class UnitVO extends BasePartUnitVO {

    /**
     * 依赖的部件id列表
     */
    private List<Long> unitList;
    /**
     * 依赖的零件id列表
     */
    private List<Long> partList;

    private PartUnitApiDetailVO unitDetail = new PartUnitApiDetailVO();


    public static UnitVO from(PartUnitDTO dto, FalconOptionsVO optionsVO, String workNo) {
        UnitVO vo = new UnitVO();
        if (dto.getYuncangId() != null) {
            vo.setId(Long.valueOf(dto.getYuncangId()));
        }
        vo.tagIds = dto.getTags().stream().map(TagVO::getId).collect(Collectors.toList());
        vo.setCode(dto.getCode());
        vo.type = "api";
        vo.version = dto.getVersion();
        vo.name = dto.getTitle();
        vo.description = optionsVO.getDescription();
        vo.sourceType = FALCON_SOURCE_TYPE;
        vo.sourceId = dto.getFalconId();
        vo.createMember = dto.getCreateUser();
        vo.title = dto.getTitle();
        vo.notes = optionsVO.getDescription();
        vo.sourceUrl = SOURCE_PREFIX + dto.getFalconId();
        vo.participant = dto.getUpdateUser();
        vo.setWorkBdId(dto.getBdId());
        vo.setWorkNo(workNo);
        vo.setTestReportId(dto.getExtraData().getTestId());
        vo.setUnitList(dto.getUnitList().stream().map(ComponentData::getId).collect(Collectors.toList()));
        vo.setPartList(dto.getPartList().stream().map(ComponentData::getId).collect(Collectors.toList()));
        vo.setCreateMember(dto.getCreateUser());
        vo.setUpdateMember(dto.getUpdateUser());

        vo.unitDetail.setUri(optionsVO.getUri());
        vo.unitDetail.setDomain(optionsVO.getServer());
        vo.unitDetail.setFalconUrl(SOURCE_PREFIX + dto.getFalconId());
        vo.unitDetail.setFalconId(dto.getFalconId());
        return vo;
    }

}
